package parsers.lectureRoom;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import parsers.CommonMethods;

import model.Building;
import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.HTMLElementName;
import net.htmlparser.jericho.Source;

public class BuildingsParser
{	
	public BuildingsParser() {}
	
	public ArrayList<Building> parse(URL url) throws IOException
	{
		ArrayList<Building> buildings = new ArrayList<Building>();
		
		Source source = new Source(url);		
		List<Element> elements = source.getAllElements(HTMLElementName.TR);
		
		for (Element element : elements)
		{
			String name = element.getContent().toString();
			if(name.contains("pId_budynek"))
			{
				List<Element> linkElements = element.getAllElements(HTMLElementName.TD);
				
				String link = linkElements.get(0).getContent().getFirstElement().getAttributeValue("href");
				String buildingName = linkElements.get(0).getContent().getFirstElement().getContent().toString();
				String buildingDescription = linkElements.get(1).getContent().toString();
				String buildingAddress = linkElements.get(2).getContent().toString();
				
				Building building = new Building(buildingName, buildingDescription, buildingAddress);
				building.setLink(CommonMethods.parseLink(link));
				buildings.add(building);
			}
		}

		return buildings;
	}
	
	/*public static void main(String[] args) throws MalformedURLException, IOException
	{
		BuildingsParser bp = new BuildingsParser();
		ArrayList<Building> list = bp.parse(new URL("file:/D:/test7.html"));
		System.out.println(list);
	}*/
}
